const path = require("node:path");
const csvWriter = require('csv-writer').createObjectCsvWriter;
const fs = require('fs');

function writeResultsToCSV(data, headerObj, fileName = 'unknown') {

    const outputDir = path.join(__dirname, '..', 'output');
    if (!fs.existsSync(outputDir)) {
        fs.mkdirSync(outputDir);
    }
    const filePath = path.join(outputDir, `${fileName}.csv`);
    const writer = csvWriter({
        path: filePath,
        header: headerObj
    });

    writer.writeRecords(data).then(() => {
        console.log(`Создан файл ${fileName}`);
    });
}

module.exports = {writeResultsToCSV}